﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QByteArray>
#include <QTimer>
#include <QFileDialog>
#include <QDataStream>
#include <QByteArray>
#include <QScreen>
#include "usbthread.h"
#include "parsehex.h"

/* 指令定义 */
#define HELLO_PC_TO_MCU         0xA0
#define HELLO_MCU_TO_PC         0xA1
#define READ16_IN_PC_TO_MCU     0xC0
#define READ16_IN_MCU_TO_PC     0xC1
#define WRITE16_IN_PC_TO_MCU    0xC2
#define WRITE16_IN_MCU_TO_PC    0xC3
#define READ32_MCU_PC_TO_MCU	0xC8
#define READ32_MCU_MCU_TO_PC	0xC9
#define WRITE32_MCU_PC_TO_MCU	0xCA
#define WRITE32_MCU_MCU_TO_PC	0xCB
#define READ64_MCU_PC_TO_MCU	0xCC
#define READ64_MCU_MCU_TO_PC	0xCD
#define WRITE64_MCU_PC_TO_MCU	0xCE
#define WRITE64_MCU_MCU_TO_PC	0xCF
#define WRITEN_MCU_PC_TO_MCU	0xD0
#define WRITEN_MCU_MCU_TO_PC	0xD1
#define WRITEA_FLASH_PC_TO_MCU	0xF2
#define WRITEN_FLASH_PC_TO_MCU	0xF6
#define WRITEN_FLASH_MCU_TO_PC	0xF7
#define READA_FLASH_PC_TO_MCU	0xF8
#define READA_FLASH_MCU_TO_PC	0xF9

/* 寄存器地址表 */
#define WADDR_MCU_PC_TO_MCU     0x10
#define RESET_MCU_MCU_TO_PC     0x90

/* 配置的默认值 */

#define DEFAULT_VENDOR_ID  0x5131
#define DEFAULT_PRODUCT_ID  0x2007
#define DEFAULT_BIN_PACKAGE_SIZE 56
#define DEFAULT_REPORT_ID 0

#define DOWNLOAD_AND_CHECK 1

#define DEBUG_WRITE_BIN_FILE 1

namespace InterCommand
{
    enum : int
    {
        HELLO        = 0,
        READ16_IN,
        WRITE16_IN,
        READ32_MCU,
        WRITE32_MCU,
        READ64_MCU,
        WRITE64_MCU,
        WRITEN_FLASH,
        READA_FLASH,
        RESET_MCU
    };
}



QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    bool eventFilter(QObject *obj, QEvent *event);

private slots:
    void redownload_data();
    void systimerUpdate(void);
    void sender_update();
    void usbreceivedata(const QByteArray);
    void usbconnect(QString dev_str);
    void usbdisconnect();
    void usbsenderror();
    void on_pushButton_openclose_clicked();
    void on_pushButton_OpenFile_clicked();
    void on_pushButton_StartDownload_clicked();
    void on_comboBox_device_currentIndexChanged(int index);
    void on_pushButton_StartDownloadFlash_clicked();
    void on_pushButton_GetMCUID_clicked();
    void on_pushButton_emptyFlash_clicked();

private:
	void check_data();
    void refreshDPI();
    void changeObjectSize(const QObject &o, double objectRate);
    void download_data();
    void Delay_MSec(unsigned int msec);
    QByteArray hexStrToByteArray(QString);//transform hex QString to hex QByteArray
    void formatString(QString &str, int n, const QChar &ch);//inserte space in every two charater
    void refresh_button();
    void send_data(uint8_t command, QByteArray & data);
    void send_bin_data(uint8_t command, QByteArray & data);
    Ui::MainWindow *ui;
    USBThread *myusbthread;
    QTimer * sender_timer, * systimer;
    bool hid_isopen = false, load_bin = false, downloading=false, write_flash=false, write_end_flash=false, checking=false, checking_end=false;
    int download_count = 0,download_end_count = 0, check_count = 0;
    uint16_t command_num = 0;
    QByteArray  last_send_data;
    int repeated_time = 0, repeated_count = 0;
    ParseHex hex_parser;
    QList<HidDeviceInfo> hid_device_list;
    QList<QString> hid_show_devices;
    QHash<QString, int> hid_device_num;
    int device_nums = 0;
    QFile save_debug_bin;
};
#endif // MAINWINDOW_H
